﻿@section css{
    <style type="text/css">
        .function-item {
            float: left;
            display: block;
            margin-left: 5px;
            margin-right: 5px;
        }

            .function-item input {
                vertical-align: bottom;
            }

        td div.mini-grid-cell-inner {
            float: left;
        }

        .mini-grid-cell .mini-tree-treecell {
            vertical-align: top;
        }
    </style>
}
<div style="width: 100%; height: 100%">
    <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
        <table style="width: 100%;">
            <tr>
                <td style="white-space: nowrap;">
                    <a class="mini-button" iconcls="icon-save" onclick="saveData()" plain="true">保存</a>
                </td>
            </tr>
        </table>
    </div>
    <div class="mini-fit">
        <div id="tree" class="mini-treegrid" style="width: 100%; height: 100%;" ajaxasync="true"
            treecolumn="MName" idfield="MID" parentfield="FID" resultastree="false"
            expandonload="true" showtreeicon="true" showcheckbox="true"
            allowselect="true" allowcellselect="false" enablehottrack="false"
            ondrawcell="ondrawcell" allowcellwrap="true">
            <div property="columns">
                <div field="MName" name="MName" headeralign="center" width="150">名称</div>
                <div field="MUrl" headeralign="center" width="220">URL</div>
                <div field="functions" headeralign="center" width="100%">按钮权限</div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    mini.parse();

    var tree = mini.get("tree");

    var UID, showAllSelect = true;;
    function setData(e) {
        e = mini.clone(e);
        UID = e.UID;
        tree.setUrl("/SystemData/User/GetAllMenuByUser?uid=" + UID);
        
    }
    tree.on("load", function (e) {
        SetCheckValue();
    })

    function SetCheckValue() {
        var _all = tree.getList();
        var _check = new Array();
        for (var i = 0; i < _all.length; i++) {
            if (_all[i].IsMID == "00000000-0000-0000-0000-000000000000") continue;
            _check.push(_all[i].IsMID);
        }
        tree.setValue(_check);
    }

    function ondrawcell(e) {
        var tree = e.sender,
            record = e.record,
            column = e.column,
            field = e.field,
            id = record[tree.getIdField()],
            funs = record.data;

        function createCheckboxs(funs) {
            if (!funs) return "";
            var html = "";
            if (showAllSelect) {
                var value = record.checkAll !== false ? "全选" : "取消";

                var clickFn = 'checkAllFunc(\'' + id + '\', this)';
                html += '<table><tr><td style="vertical-align:top;"><input onclick="' + clickFn + '" type="button" value="' + value + ' " class="function-item" style="border:solid 1px #aaa;" /></td><td>';
            }
            for (var i = 0, l = funs.length; i < l; i++) {
                var fn = funs[i];
                var clickFn = 'checkFunc(\'' + id + '\',\'' + fn.ID + '\', this.checked)';
                var checked = fn.IsChecked ? 'checked' : '';
                html += '<label class="function-item"><input onclick="' + clickFn + '" ' + checked + ' type="checkbox" name="'
                        + fn.ID + '" hideFocus />' + fn.Text + '</label>';
            }
            html += '</td></tr></table>';
            return html;
        }

        if (field == 'functions') {
            e.cellHtml = createCheckboxs(funs);
        }

    }

    function checkFunc(id, action, checked) {
        var record = tree.getRecord(id);
        if (!record) return;
        var funs = record.data;
        if (!funs) return;
        function getAction(action) {
            for (var i = 0, l = funs.length; i < l; i++) {
                var o = funs[i];
                if (o.ID == action) return o;
            }
        }
        var obj = getAction(action);
        if (!obj) return;
        obj.IsChecked = checked;
    }

    function checkAllFunc(id, btn) {
        var record = tree.getRecord(id);
        if (!record) return;
        var funs = record.data;
        if (!funs) return;


        var checked = record.checkAll !== false;

        for (var i = 0, l = funs.length; i < l; i++) {
            var o = funs[i];
            o.IsChecked = checked;
        }

        record.checkAll = !checked;
        tree.updateRow(record);

    }

    function saveData() {
        var modified = tree.getCheckedNodes();//选中值
        var jsonEdit = mini.encode(modified)//把数组转换为JSON
        jsonEdit = escape(jsonEdit);//编码
        tree.loading("正在保存");
        $.ajax({
            url: "/SystemData/User/SaveUserMenu",
            type: "post",
            data: { edit: jsonEdit, uid: UID },
            success: function (text, flag, res) {
                tree.unmask();
                if (res.responseText == "") {
                    top.mini.showTips({ content: "保存成功", state: "info", x: "center", y: "center", timeout: 1000 });
                } else {
                    $.tip({ text: "保存失败", staytime: 1000 });
                }
            },
            error: function (text, flag, res) {
                $.tip({ text: "保存失败", staytime: 1000 });
            }
        });


    }

</script>
